import { createStore } from 'vuex'
import menu from './modules/menu'
import role from './modules/role'
import user from './modules/user'

export default createStore({
  state: {
    userinfo: sessionStorage.getItem('userinfo') ? JSON.parse(sessionStorage.getItem('userinfo') || '0') : null
  },
  getters: {
    getUser(state: any) {
      return state.userinfo
    }
  },
  mutations: {
    REQ_USER(state: any, payload: any) {
      if (payload) {
        state.userinfo = payload
        sessionStorage.setItem('userinfo', JSON.stringify(payload))
      } else {
        sessionStorage.removeItem('userinfo')
      }
    }
  },
  actions: {
    UserActions({ commit }: any, payload: any) {
      commit('REQ_USER', payload)
    }
  },
  modules: {
    menu,
    role,
    user
  }
})
